
package com.yytek.mqtt.impl;

import com.yytek.common.client.base.MqMessage;
import com.yytek.common.client.net.IotMqttClient;
import com.yytek.common.client.net.IotMqttContext;
import com.yytek.common.client.net.MqttCallback;
import com.yytek.common.core.domain.R;
import com.yytek.common.core.domain.AttributeInfo;
import com.yytek.common.core.domain.Device;
import com.yytek.common.web.domain.ThingModelMessage;
import com.yytek.gw.service.DriverCustomService;
import com.yytek.gw.service.DriverMsgCommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;


/**
 * 驱动实现
 */
@Slf4j
@Service
public class DriverCustomServiceImpl implements DriverCustomService, MqttCallback {

    @Value("${driver.port}")
    private Integer driverPort;

    @Resource
    public IotMqttClient iotMqttClient;
    @Resource
    public DriverMsgCommonService driverMsgCommonService;
    @Override
    public void initial() {
        //初始化监听mqtt所有topic
        //事件监听
        iotMqttClient.eventPostScribe(this);
        //历史监听
        iotMqttClient.historyPostScribe(this);
        //批量上报属性监听
        iotMqttClient.packPostScribe(this);
        //属性获取监听
        iotMqttClient.propertyGetReplySubscribe(this);
        //设置属性监听
        iotMqttClient.propertySetReplySubscribe(this);
        //服务调用监听
        iotMqttClient.serviceInvokeReplySubscribe(this);
        //子设备登录监听
        iotMqttClient.subLoginSubscribe(this);
        //子设备下线监听
        iotMqttClient.subLogoutSubscribe(this);
        //属性上报监听
        iotMqttClient.subPropertyPostSubscribe(this);
    }

    @Override
    public R read(Map<String, AttributeInfo> driverInfo, Map<String, AttributeInfo> dataInfo, Device device) throws Exception {
        // 因为 MQTT 的数据来源是被动接收的，所以无需实现该 Read 方法
        // 接收数据处理函数在 onMessageReceived
        return R.ok();
    }

    @Override
    public R  write(Map<String, AttributeInfo> driverInfo, Map<String, AttributeInfo> dataInfo, Device device) throws Exception {
        String deviceId = device.getIotId();
        Object sendMsg = null;
        //是否任务发送
        if(dataInfo.get("isTask").getValue() != null){
            //直接发送
            sendMsg =  dataInfo.get("data").getValue();
        } else{
            //编码后的数据
            sendMsg = driverMsgCommonService.anaylisToMsg(deviceId, dataInfo.get("data").getValue());
        }
        // TODO 向下发送数据
        iotMqttClient.publish(dataInfo.get("topic").getValue().toString(),sendMsg.toString());
        return R.ok();
    }

    /**
     * 订阅后收到消息读
     * @param context
     * @param msg
     */
    @Override
    public void onMessageReceived(IotMqttContext context, MqMessage msg) {
//        System.out.println("---topic----"+context.getTopic());
//        System.out.println("---getProductId----"+context.getProductId());
//        System.out.println("---getDeviceName----"+context.getDeviceName());
//        System.out.println("---msgid----"+msg.getMessageId());
//        System.out.println("---msg----"+msg.getJsonObject().toJSONString());
        //调用统一解析脚本
        driverMsgCommonService.sendToCommonHandleMsg(context.getDeviceName(),msg.getJsonObject());
    }
}
